package com.tjdr.his.drugstore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tjdr.his.common.result.HttpResp;
import com.tjdr.his.common.result.ResultCode;
import com.tjdr.his.domain.entity.DrugInfo;
import com.tjdr.his.drugstore.service.IDrugInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @AUTHOR 张三
 * @DATE 2024/10/16 10:45
 * @DESCRIBER
 */
@RestController
@RequestMapping("/api/drug")
@Slf4j
public class DrugInfoController {
    @Resource(name = "drugService")
    private IDrugInfoService drugService;
    @GetMapping("/getDrugInfoByPropertiesHunHe")
    public HttpResp getDrugInfoByProperties(@RequestBody DrugInfo drugInfo, @RequestParam(defaultValue = "10") int pageSize, @RequestParam(defaultValue = "1")  int currentPage){
        log.debug("durgInfo:{},pageSize:{},currentPage:{}",drugInfo,pageSize,currentPage);
       IPage page = drugService.selectDrugsByProperties(drugInfo,pageSize,currentPage);
       Map map = new HashMap<>();
       map.put("list",page.getRecords());
       map.put("total",page.getTotal());
       HttpResp httpResp=  HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,map);
        log.debug("httpResp:{}",httpResp);
        return httpResp;

    }

    @GetMapping("/getDrugInfoByPropertiesMap")
    public HttpResp getDrugInfoByPropertiesMap(@RequestBody Map<String,String> map){
        log.debug("map:{}",map);
        DrugInfo drugInfo = new DrugInfo();
        drugInfo.setDrugType(map.get("drugType"));
        drugInfo.setDrugName(map.get("drugName"));
        drugInfo.setMnemonicCode(map.get("mnemonicCode"));
        Integer pageSize = map.get("pageSize")==null?10:Integer.valueOf(map.get("pageSize"));
        Integer currentPage = map.get("currentPage")==null?1:Integer.valueOf(map.get("currentPage"));
        IPage page = drugService.selectDrugsByProperties(drugInfo,pageSize,currentPage);
        Map resultMap = new HashMap<>();
        resultMap.put("list",page.getRecords());
        resultMap.put("total",page.getTotal());
        HttpResp httpResp=  HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,resultMap);
        log.debug("httpResp:{}",httpResp);
        return httpResp;

    }

    @GetMapping("/getDrugInfoByProperties")
    public HttpResp getDrugInfoByProperties(@RequestBody DrugInfo drugInfo){
        log.debug("durgInfo:{}",drugInfo);
        Integer pageSize = drugInfo.getPageSize()==null?10:drugInfo.getPageSize();
        Integer currentPage = drugInfo.getCurrentPage()==null?1:drugInfo.getCurrentPage();
        IPage page = drugService.selectDrugsByProperties(drugInfo,pageSize,currentPage);
        Map map = new HashMap<>();
        map.put("list",page.getRecords());
        map.put("total",page.getTotal());
        HttpResp httpResp=  HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,map);
        log.debug("httpResp:{}",httpResp);
        return httpResp;

    }
    @PutMapping("updateDrugInfoById")
    public HttpResp updateDrugInfoById(@RequestBody DrugInfo drugInfo){
        log.debug("drugInfo:{}",drugInfo);
        boolean result = drugService.insertOrupdateDrugById(drugInfo);
        HttpResp httpResp = HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,result);
        log.debug("httpResp:{}",httpResp);
        return  httpResp;
    }

    @PutMapping("insertDrugInfo")
    public HttpResp insertDrugInfo(@RequestBody DrugInfo drugInfo){
        log.debug("drugInfo:{}",drugInfo);
        boolean result = drugService.insertOrupdateDrugById(drugInfo);
        HttpResp httpResp = HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,result);
        log.debug("httpResp:{}",httpResp);
        return  httpResp;
    }
}
